#include <gtk/gtk.h>
#include <mysql/my_global.h>
#include <mysql/mysql.h>
#include <stdlib.h>
#define db "data" //database yang digunakan
#define host "localhost" //host dari database
#define user "data" //nama user mysql
#define pass "pudingmu" //password user mysql

//variabel2 mysql
MYSQL_ROW row;
MYSQL_RES *res_result;
MYSQL *conn;

//variabel dan widget tampilan utama
GtkBuilder *builder;
GtkWidget *window,*statusMain,*statusTable,*cover;
GtkWidget *kolView,*kol_scrolled_win,*searchEntry,*searchBtn;
GtkWidget *togJudul,*togDesk,*togPen,*togTeks;
GtkWidget *awal,*prev,*next,*end;
GtkDialog *dialog2,*aboutdialog,*jenis,*kategori,*penerbit,*info,*koleksi;
GtkTreeView *viewJenis;
guint statusbar_context_id;
guint statustable_context_id;

//kebutuhan untuk list2
int num_fields,num_rows,tmbh,col;

//widget dan variabel addKoleksi
GtkWidget *tahun,*entryID,*entryJdl,*desText;
GtkWidget *cbKategori,*cbPenerbit,*cbJenis,*fileChooser;
GtkWidget *btnOkKol,*btnBatalKol;
gchar *filename;

//widget dan variabel JenisList
GtkWidget *jenisView,*scrolled_win;
GtkWidget *entryIDjns,*entryNamaJns;
GtkWidget *btnSimpanJns,*btnBatalJns;
GtkWidget *btnTambahJns,*btnEditJns,*btnHapusJns,*btnCloseJns;
GtkTreeSelection *selection;

//widget dan variabel penerbit
GtkWidget *penView,*pen_scrolled_win;
GtkWidget *entryIDPen,*entryNamaPen,*penTextview;
GtkWidget *btnSimpanPen,*btnBatalPen;
GtkWidget *btnTambahPen,*btnEditPen,*btnHapusPen,*btnClosePen;
GtkTreeSelection *pen_selection;

//widget dan variabel kategori
GtkWidget *katView,*kat_scrolled_win;
GtkWidget *entryIDKat,*entryNamaKat,*katTextview;
GtkWidget *btnSimpanKat,*btnBatalKat;
GtkWidget *btnTambahKat,*btnEditKat,*btnHapusKat,*btnCloseKat;
GtkTreeSelection *kat_selection;

void
on_entry1_activate(GtkWidget*);



void
on_help_clicked(GtkToolButton*);

void
on_window1_show(GtkWidget *widget, gpointer user_data);

void
on_server_clicked(GtkWidget *widget, gpointer user_data);

void
on_admin_clicked(GtkWidget *widget, gpointer user_data);

void
on_preference_clicked(GtkWidget *widget,gpointer user_data);

void
on_add_clicked(GtkWidget *widget, gpointer user_data);

void
on_edit_clicked(GtkWidget *widget, gpointer user_data);

void
on_window1_destroy(GtkWidget *widget, gpointer user_data);

void
on_exit_clicked(GtkToolButton *widget, gpointer user_data);

void
on_about_clicked();

void
on_awal_clicked();

void
on_prev_clicked();

void
on_next_clicked();



void
on_cari_clicked(GtkWidget*,gpointer);

void
print_error (MYSQL *conn, char *message);

//list untuk jenis
//deklarasi nama kolom yang akan digunakan
enum
{
    ID_JENIS = 0,
    JENIS,
    jCOLUMNS
};
//membuat list dari jenis
static GtkTreeModel*
createJenislist()
{
    GtkListStore *store;
    GtkTreeIter iter;

    store = gtk_list_store_new (jCOLUMNS, G_TYPE_STRING, G_TYPE_STRING);

    mysql_query(conn, "select * from jenis");
    res_result=mysql_store_result(conn);
    num_fields=mysql_num_fields(res_result);
    num_rows=mysql_num_rows(res_result);
    while ((row=mysql_fetch_row(res_result)))
    {
        //menampilan row sebagai kolom yaitu pada row[0] dan row[1]
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            ID_JENIS, row[0],
                            JENIS, row[1],
                            -1);
    }

    mysql_free_result(res_result);
    //mysql_close(conn);
    return GTK_TREE_MODEL (store);
}

//static
GtkWidget*
setup_jenis_view ();

//callback jenis
void
on_btnSimpanJns_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnBatalJns_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnHapusJns_clicked(GtkWidget *widget, gpointer selection);

//Penerbit list
enum
{
    ID_PEN = 0,
    PENERBIT,
    ALAMAT,
    pCOLUMNS
};

static GtkTreeModel*
createPenlist()
{
    GtkListStore *store;
    GtkTreeIter iter;

    store = gtk_list_store_new (pCOLUMNS, G_TYPE_STRING, G_TYPE_STRING,G_TYPE_STRING);

    mysql_query(conn, "select * from penerbit");
    res_result=mysql_store_result(conn);
    num_fields=mysql_num_fields(res_result);
    num_rows=mysql_num_rows(res_result);
    while ((row=mysql_fetch_row(res_result)))
    {
        //menampilan row sebagai kolom yaitu pada row[0] dan row[1]
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            ID_PEN, row[0],
                            PENERBIT, row[1],
                            ALAMAT,row[2],
                            -1);
    }

    mysql_free_result(res_result);
    //mysql_close(conn);
    return GTK_TREE_MODEL (store);
}

//static
static GtkWidget*
setup_pen_view ()
{
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    GtkTreeModel *model;
    GtkWidget *pentreeview;

    pentreeview = gtk_tree_view_new ();
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("ID", renderer, "text", ID_PEN, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (pentreeview), column);

    //render column 2
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Penerbit", renderer, "text", PENERBIT, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (pentreeview), column);

    //render column 3
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Alamat", renderer, "text", ALAMAT, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (pentreeview), column);

    model=createPenlist();

    /* Add the tree model to the tree view and unreference it so that the model will
    * be destroyed along with the tree view. */
    gtk_tree_view_set_model (GTK_TREE_VIEW (pentreeview), model);
    g_object_unref (model);
    return pentreeview;
}

//Kategori list
enum
{
    ID_KAT = 0,
    KATEGORI,
    KETERANGAN,
    kCOLUMNS
};

static GtkTreeModel*
createKatlist()
{
    GtkListStore *store;
    GtkTreeIter iter;
    //conn= mysql_init(NULL);
    store = gtk_list_store_new (kCOLUMNS, G_TYPE_STRING, G_TYPE_STRING,G_TYPE_STRING);

    mysql_query(conn, "select * from kategori");
    res_result=mysql_store_result(conn);
    num_fields=mysql_num_fields(res_result);
    num_rows=mysql_num_rows(res_result);
    while ((row=mysql_fetch_row(res_result)))
    {
        //menampilan row sebagai kolom yaitu pada row[0] dan row[1]
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            ID_KAT, row[0],
                            KATEGORI, row[1],
                            KETERANGAN,row[2],
                            -1);
    }

    mysql_free_result(res_result);
    //mysql_close(conn);
    return GTK_TREE_MODEL (store);
}

//static
static GtkWidget*
setup_kat_view ()
{
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    GtkTreeModel *model;
    GtkWidget *kattreeview;

    kattreeview = gtk_tree_view_new ();
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("ID", renderer, "text", ID_KAT, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kattreeview), column);

    //render column 2
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Kategori", renderer, "text", KATEGORI, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kattreeview), column);

    //render column 3
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes
             ("Keterangan", renderer, "text", KETERANGAN, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (kattreeview), column);

    model=createKatlist();

    /* Add the tree model to the tree view and unreference it so that the model will
    * be destroyed along with the tree view. */
    gtk_tree_view_set_model (GTK_TREE_VIEW (kattreeview), model);
    g_object_unref (model);
    return kattreeview;
}

//tombol kategori
void
on_btnTambahKat_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnSimpanKat_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnBatalKat_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnEditKat_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnHapusKat_clicked(GtkWidget *widget, gpointer selection);

void
on_btnCloseKat_clicked(GtkWidget* widget, gpointer user_data);


//tombol penerbit
void
on_btnTambahPen_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnSimpanPen_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnBatalPen_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnEditPen_clicked(GtkWidget *widget, gpointer user_data);

void
on_btnHapusPen_clicked(GtkWidget *widget, gpointer selection);

void
on_btnClosePen_clicked(GtkWidget* widget, gpointer user_data);


//koleksi

//Kategori list
enum
{
    ID_KOL = 0,
    JUDUL,
    kolJENIS,
    kolKATEGORI,
    kolPENERBIT,
    TAHUN,
    DESKRIPSI,
    kolCOLUMNS
};


//select main row
gboolean
view_selection_func (GtkTreeSelection *selection,
                     GtkTreeModel     *model,
                     GtkTreePath      *path,
                     gboolean          path_currently_selected,
                     gpointer          userdata);

void
on_btnBatalKol_clicked(GtkWidget *widget,gpointer user_data);
